package thread.newCachedThreadPool;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//方法1
class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("继承Thread，重写run方法创建线程");
    }
}

//方法2
class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("实现Runnable接口，重写run方法");
    }
}

//方法6
class MyCallableTest implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("创建线程:" + Thread.currentThread().getName());
        return 2;
    }
}

public class Main {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> task = new FutureTask<>(new MyCallableTest());
        Thread thread = new Thread(task);
        thread.start();
        System.out.println("创建线程的返回结果为:" + task.get());
    }

    public static void main5(String[] args) {
        Thread thread = new Thread(() -> {
           System.out.println("使用lambda表示创建线程");
        });
        thread.start();
    }

    public static void main4(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("使用匿名内部类，实例Runnable接口作为构造参数");
            }
        });
        thread.start();
    }

    public static void main3(String[] args) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println("使用匿名内部类创建 Thread 子类对象");
            }
        };
        thread.start();
    }

    public static void main2(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();
    }

    //方法1
    public static void main1(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
    }
}
